package com.beiyou.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beiyou.dao.GoodsMapper;
import com.beiyou.dto.GoodsDto;
import com.beiyou.dto.GoodsDtoQuery;
import com.beiyou.entity.GoodsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wzby
 * @Date: 2022/10/21 9:58
 */
@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    public Integer insert(GoodsDto goodsDto){
        GoodsEntity goodsEntity = BeanUtil.copyProperties(goodsDto, GoodsEntity.class);
        return goodsMapper.insert(goodsEntity);
    }

    public Integer statusUpdate(Integer id, Integer status){
        return goodsMapper.statusUpdate(id, status);
    }

    public List<GoodsDto> selectXml(GoodsDtoQuery goodsDtoQuery){
        List<GoodsEntity> entities = goodsMapper.selectXml(goodsDtoQuery);
        return BeanUtil.copyToList(entities,GoodsDto.class);
    }
    public Integer update(GoodsDto goodsDto){
        GoodsEntity goodsEntity = BeanUtil.copyProperties(goodsDto, GoodsEntity.class);
        return goodsMapper.update(goodsEntity);
    }

    public List<GoodsDto> select(GoodsDtoQuery goodsDtoQuery){
        LambdaQueryWrapper<GoodsEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(goodsDtoQuery.getParentId() != null, GoodsEntity::getParentId,goodsDtoQuery.getParentId());
        wrapper.eq(goodsDtoQuery.getId() != null, GoodsEntity::getId,goodsDtoQuery.getId());
        wrapper.eq(goodsDtoQuery.getStatus() != null, GoodsEntity::getId,goodsDtoQuery.getStatus());
        wrapper.eq(goodsDtoQuery.getName() != null, GoodsEntity::getId,goodsDtoQuery.getName());
        List<GoodsEntity> goodsEntityList = goodsMapper.selectList(wrapper);
        List<GoodsDto> goodsDto = BeanUtil.copyToList(goodsEntityList, GoodsDto.class);
        return goodsDto;
    }
}
